<?php
/**
 * Created by PhpStorm.
 * User: fdg296
 * Date: 2017/8/3
 * Time: 14:16
 */
namespace backend\models;

use common\helps\tools;
use linslin\yii2\curl\Curl;
use yii\base\Model;
use yii\web\UploadedFile;

class UploadForm extends Model
{
    /**
     * @var UploadedFile file attribute
     */
    public $img_url;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['img_url'], 'file', 'extensions' => ['png', 'jpg', 'gif', 'txt', 'wmv', 'mp4','x-ms-wmv'], 'maxSize' => 1024*1024*1024],
        ];
    }

    public function upload()
    {

        if ($this->validate())
        {
            $re = $this->saveAs($this->img_url->extension);        //调用内置封装类**执行上传
			if(tools::arrayHasValue($re['data'],'hostUrl')){
                return $re['data']['hostUrl']; //上传成功**返回文件的路径名称
            }else{
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    //多图上传
    public function uploads(){
        if ($this->validate()){
            $img_list = array();
            foreach($this->img_url as $key=>$file){
                $rootPath = "uploads/"; //定义上传的根目录
                $ext = $file->extension;   //获取文件的后缀(*格式*)
                $randName = time() . rand(1000, 99999) . "." . $ext; //重新编译文件名称
                $path = date('Y-m-d',time());
                $rootPath = $rootPath . $path . "/";    //拼接
                $bath_path = BASE_PATH . 'public/' . $rootPath;
                if (!file_exists($bath_path)){   //判断该目录是否存在
                    mkdir($bath_path,true);
                    chmod($bath_path,0777);
                }
                $re = $file->saveAs($bath_path . $randName);//调用内置封装类**执行上传
                if($re){
                    $img_list[] = $rootPath . $randName; //上传成功**返回文件的路径名称
                }else{
                    return false;
                }
            }
            return $img_list;
        }else{
            return false;
        }
    }

	/**
	 * @param $fileExt Curl保存图片地址
	 * @param bool $deleteTempFile
	 * @return mixed
	 */
	public function saveAs($fileExt, $deleteTempFile = true)
	{
		if($deleteTempFile) {
			$curl = new Curl();
			if(class_exists('CURLFile')) {
				$file = curl_file_create($this->img_url->tempName);//new CURLFile($url);
				// 禁用"@"上传方法,这样就可以安全的传输"@"开头的参数值
				$curl->setOption(CURLOPT_SAFE_UPLOAD,true);
				//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
			} else {
				$file = "@{$this->img_url->tempName}";
			}

            $response = $curl->setOptions(
				[
					CURLOPT_RETURNTRANSFER=>1,
					CURLOPT_POST=>1,
					CURLOPT_POSTFIELDS=>[
						'material'=>$file,		//临时文件
						'fileExt'=>$fileExt		//文件后缀
					],
				]
			)->post(\Yii::$app->params['imageCurlUrl']);
			return json_decode($response,true);
		}else{
			return copy($this->tempName, $this->img_url->tempName);
		}
	}
}